home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / sunbird / js / calWeekPrinter.js < prev    next >
Encoding:
JavaScript  |  2007-05-23  |  11.6 KB  |  300 lines

  1. /* -*- Mode: javascript; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  * ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Mozilla Calendar code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  *   Joey Minta <jminta@gmail.com>
  19.  * Portions created by the Initial Developer are Copyright (C) 2006
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Matthew Willis <lilmatt@mozilla.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. /**
  40.  * Prints a two column view of a week of events, much like a paper day-planner
  41.  */
  42.  
  43. function calWeekPrinter() {
  44.     this.wrappedJSObject = this;
  45. }
  46.  
  47. calWeekPrinter.prototype.QueryInterface =
  48. function QueryInterface(aIID) {
  49.     if (!aIID.equals(Components.interfaces.nsISupports) &&
  50.         !aIID.equals(Components.interfaces.calIPrintFormatter)) {
  51.         throw Components.results.NS_ERROR_NO_INTERFACE;
  52.     }
  53.  
  54.     return this;
  55. };
  56.  
  57. calWeekPrinter.prototype.getName =
  58. function weekPrint_getName() {
  59.     return calGetString("calendar", "weekPrinterName");
  60. };
  61. calWeekPrinter.prototype.__defineGetter__("name", calWeekPrinter.prototype.getName);
  62.  
  63. calWeekPrinter.prototype.formatToHtml =
  64. function weekPrint_format(aStream, aStart, aEnd, aCount, aItems, aTitle) {
  65.     // Create the e4x framework of the HTML document
  66.     var html = <html/>;
  67.     html.appendChild(
  68.             <head>
  69.                 <title>{aTitle}</title>
  70.                 <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
  71.                 <style type='text/css'/>
  72.             </head>);
  73.     html.head.style = ".main-table { font-size: 26px; font-weight:bold; }\n";
  74.     html.head.style += ".day-name { border: 1px solid #000; background-color: #e0e0e0; font-size: 12px; font-weight: bold; }\n";
  75.  
  76.     var body = <body/>;
  77.  
  78.     // helper: returns the passed item's startDate, entryDate or dueDate, in
  79.     //         that order. If the item doesn't have one of those dates, this
  80.     //         doesn't return.
  81.     function hasUsableDate(item) {
  82.         return item.startDate || item.entryDate || item.dueDate;
  83.     }
  84.  
  85.     // Clean out the item list so it only contains items we will want to
  86.     // include in the printout.
  87.     var filteredItems = aItems.filter(hasUsableDate);
  88.  
  89.     var calIEvent = Components.interfaces.calIEvent;
  90.     var calITodo = Components.interfaces.calITodo
  91.     function compareItems(a, b) {
  92.         // Sort tasks before events
  93.         if (a instanceof calIEvent && b instanceof calITodo) {
  94.             return 1;
  95.         }
  96.         if (a instanceof calITodo && b instanceof calIEvent) {
  97.             return -1;
  98.         }
  99.         if (a instanceof calIEvent) {
  100.             var startCompare = a.startDate.compare(b.startDate);
  101.             if (startCompare != 0) {
  102.                 return startCompare;
  103.             }
  104.             return a.endDate.compare(b.endDate);
  105.         }
  106.         var dateA = a.entryDate || a.dueDate;
  107.         var dateB = b.entryDate || b.dueDate;
  108.         return dateA.compare(dateB);
  109.     }
  110.     var sortedList = filteredItems.sort(compareItems);
  111.  
  112.     var weekFormatter = Components.classes["@mozilla.org/calendar/weektitle-service;1"]
  113.                                   .getService(Components.interfaces.calIWeekTitleService);
  114.  
  115.     // A place to cache the string for "All Day" events later if we need it.
  116.     var mAllDayString = null;
  117.  
  118.     // Start at the beginning of the week that aStart is in, and loop until
  119.     // we're at aEnd. In the loop we build the HTML table for each day, and
  120.     // get the day's items using getDayTd().
  121.     var start = aStart || sortedList[0].startDate || sortedList[0].entryDate ||
  122.                 sortList[0].dueDate;
  123.     ASSERT(start, "can't find a good starting date to print");
  124.  
  125.     var lastItem = sortedList[sortedList.length-1];
  126.     var end = aEnd || lastItem.startDate || lastItem.entryDate ||
  127.                lastItem.dueDate;
  128.     ASSERT(end, "can't find a good ending date to print");
  129.  
  130.     var date = start.startOfWeek;
  131.     var startOfWeek = getPrefSafe("calendar.week.start", 0);
  132.     date.day += startOfWeek;
  133.     date.normalize();
  134.     // Make sure we didn't go too far ahead
  135.     if (date.compare(start) == 1) {
  136.         date.day -= 7;
  137.         date.normalize();
  138.     }
  139.  
  140.     while(date.compare(end) == -1) {
  141.         var weekno = weekFormatter.getWeekTitle(date);
  142.         var weekTitle = calGetString("calendar", 'WeekTitle', [weekno]);
  143.         body.appendChild(
  144.                      <table border='0' width='100%' class='main-table'>
  145.                          <tr> 
  146.                              <td align='center' valign='bottom'>{weekTitle}</td>
  147.                          </tr>
  148.                      </table>);
  149.         var mainWeek = <table width='100%' height="90%" border='solid 1px;'/>
  150.  
  151.         // Create the <td> for each day, and put it into an array.
  152.         var dayTds = new Array();
  153.         for (var i = 0; i < 7 ; i++) {
  154.             dayTds[date.weekday] = this.getDayTd(date, sortedList);
  155.             date.day += 1;
  156.             date.normalize();
  157.         }
  158.  
  159.         var monRow = <tr height="33%"/>;
  160.         monRow.appendChild(dayTds[1]); // Monday
  161.         monRow.appendChild(dayTds[4]); // Thursday
  162.         mainWeek.appendChild(monRow);
  163.  
  164.         var tueRow = <tr height="33%"/>;
  165.         tueRow.appendChild(dayTds[2]); // Tuesday
  166.         tueRow.appendChild(dayTds[5]); // Friday
  167.         mainWeek.appendChild(tueRow);
  168.  
  169.         var wedRow = <tr height="33%"/>;
  170.         wedRow.appendChild(dayTds[3]); // Wednesday
  171.  
  172.         // Saturday and Sunday are half-size
  173.         var satSunTd = <td height="33%"/>;
  174.         var weekendTable = <table border="1" width="100%" height="100%"/>;
  175.  
  176.         var satRow = <tr valign='top'/>;
  177.         satRow.appendChild(dayTds[6]); // Saturday
  178.         weekendTable.appendChild(satRow);
  179.  
  180.         var sunRow = <tr valign='top'/>;
  181.         sunRow.appendChild(dayTds[0]); // Sunday
  182.         weekendTable.appendChild(sunRow);
  183.  
  184.         satSunTd.appendChild(weekendTable);
  185.         wedRow.appendChild(satSunTd);
  186.         mainWeek.appendChild(wedRow);
  187.  
  188.         body.appendChild(mainWeek);
  189.         // Make sure each month gets put on its own page
  190.         body.appendChild(<br style="page-break-after: always;"/>);
  191.     }
  192.     html.appendChild(body);
  193.  
  194.     // Stream out the resulting HTML
  195.     var convStream = Components.classes["@mozilla.org/intl/converter-output-stream;1"]
  196.                                .getService(Components.interfaces.nsIConverterOutputStream);
  197.     convStream.init(aStream, 'UTF-8', 0, 0x0000);
  198.     convStream.writeString(html.toXMLString());
  199. };
  200.  
  201. calWeekPrinter.prototype.getDayTd =
  202. /**
  203.  * Given a calIDateTime and an array of items, this function creates an HTML
  204.  * table containing the items, using the appropriate formatting and colours.
  205.  */
  206. function weekPrint_getDayTable(aDate, aItems) {
  207.     // mainTd is the <td> element from the parent HTML table that will hold
  208.     // the child HTML tables containing the date string and this day's items.
  209.     var mainTd = <td border='1px solid black;' width="50%" valign='top'/>
  210.     var dateFormatter = Components.classes["@mozilla.org/calendar/datetime-formatter;1"]
  211.                                   .getService(Components.interfaces.calIDateTimeFormatter);
  212.     var dateString = dateFormatter.formatDateLong(aDate);
  213.  
  214.     // Add the formatted date string (in its own child HTML table)
  215.     mainTd.appendChild(
  216.                      <table class='day-name' width='100%' style='border: 1px solid black;'>
  217.                          <tr> 
  218.                              <td align='center' valign='bottom'>{dateString}</td>
  219.                          </tr>
  220.                      </table>);
  221.  
  222.     // Add the formatted items (in their child HTML table)
  223.     var innerTable = <table valign='top' style='font-size: 10px;'/>;
  224.     for each (var item in aItems) {
  225.         var sDate = item.startDate || item.entryDate || item.dueDate;
  226.         var eDate = item.endDate || item.dueDate || item.entryDate;
  227.  
  228.         // End dates are exclusive. Adjust the eDate accordingly.
  229.         if (sDate && sDate.isDate && eDate) {
  230.             eDate = eDate.clone();
  231.             eDate.day -= 1;
  232.             eDate.normalize();
  233.         }
  234.  
  235.         // If the item has no end date, or if the item's end date is aDate or
  236.         // is before aDate, skip to the next item.
  237.         if (!eDate || (eDate.compare(aDate) < 0)) {
  238.             continue;
  239.         }
  240.  
  241.         // No start date or a start date that's after the date we want is bad.
  242.         if (!sDate || (sDate.compare(aDate) > 0)) {
  243.             break;
  244.         }
  245.  
  246.         function getStringForDate(aDate) {
  247.             if (!aDate.isDate) {
  248.                 return dateFormatter.formatTime(aDate);
  249.             }
  250.  
  251.             // We cache the string for "All Day" 'cause we're good like that.
  252.             if (this.mAllDayString == null) {
  253.                 this.mAllDayString = calGetString("dateFormat", "AllDay");
  254.             }
  255.             return this.mAllDayString;
  256.         }
  257.  
  258.         var time;
  259.         if (sDate && eDate) {
  260.             if (!sDate.isDate) {
  261.                 time = getStringForDate(sDate) + '-' + getStringForDate(eDate);
  262.             } else {
  263.                 time = getStringForDate(sDate);
  264.             }
  265.         } else if (sDate) {
  266.             time = getStringForDate(sDate);
  267.         } else if (eDate) {
  268.             time = getStringForDate(eDate);
  269.         }
  270.  
  271.         // Get calendar and category colours and apply them to the item's
  272.         // table cell.
  273.         var calMgr = Components.classes["@mozilla.org/calendar/manager;1"]
  274.                                .getService(Components.interfaces.calICalendarManager);
  275.         var calColor = calMgr.getCalendarPref(item.calendar, 'color');
  276.         if (!calColor) {
  277.             calColor = "#A8C2E1";
  278.         }
  279.         var pb2 = Components.classes["@mozilla.org/preferences-service;1"]
  280.                             .getService(Components.interfaces.nsIPrefBranch2);
  281.         var catColor;
  282.         try {
  283.             catColor = pb2.getCharPref("calendar.category.color."+item.getProperty("CATEGORIES").toLowerCase());
  284.         } catch(ex) {}
  285.  
  286.         var style = 'font-size: 11px; background-color: ' + calColor + ';';
  287.         style += ' color: ' + getContrastingTextColor(calColor);
  288.         if (catColor) {
  289.             style += ' border: solid ' + catColor + ' 2px;';
  290.         }
  291.         var item = <tr>
  292.                        <td valign='top' align='left' style={style}>{time} {item.title}</td>
  293.                    </tr>;
  294.         innerTable.appendChild(item);
  295.     }
  296.     innerTable.appendChild(<p> </p>);
  297.     mainTd.appendChild(innerTable);
  298.     return mainTd;
  299. };
  300.